package controller;

import pojo.Book;
import pojo.User;
import service.BookService;
import service.UserService;
import view.AddBook;
import view.QueryView;
import view.SignIn;


import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class AddBookListener implements ActionListener {
    private AddBook addBook;
    private QueryView queryView;
    public AddBookListener(AddBook dialog,QueryView queryView){
        super();
        this.addBook = dialog;
        this.queryView=queryView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("返回")) {
            addBook.dispose();
        }
        if(e.getActionCommand().equals("新增")) {
            String bookId = addBook.getTfId().getText();
            String bookName = addBook.getTfName().getText();
            String writer = addBook.getTfWriter().getText();
            String page = addBook.getTfPage().getText();
            String price = addBook.getTfPrice().getText();
            String press= addBook.getTfPress().getText();
            String classification = (String) addBook.getCBType().getSelectedItem();
            String total = addBook.getTfTotal().getText();
            String lend=null;
            String surplus = total;
            String type = "普通用户";
            Book book = new Book(bookId, bookName, writer, page,price, press, classification,total,lend,surplus);
            BookService bookService = new BookService();
            bookService.add(book);
            int a = JOptionPane.showConfirmDialog(addBook, "确认新增?", "确认信息", JOptionPane.YES_NO_OPTION);
            if (a == JOptionPane.YES_OPTION) {
                addBook.dispose();
                List<Book> bookList = bookService.searchAll();
                queryView.refresh(bookList);
            }
        }
    }
}
